package com.qzsoft.zhjd.machine.zhgcloud.refuelrecords;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.zhjd.machine.modules.machinefuel.entity.MachineRefuelRecordsEntity;
import com.qzsoft.zhjd.machine.modules.machinefuel.service.MachineRefuelRecordsService;
import com.qzsoft.zhjd.machine.modules.machineinfo.entity.MachineInfoEntity;
import com.qzsoft.zhjd.machine.modules.machineinfo.service.MachineInfoService;
import com.qzsoft.zhjd.machine.zhgcloud.ZhgHandler;
import com.qzsoft.zhjd.machine.zhgcloud.ZhgResultDataCallback;
import com.qzsoft.zhjd.machine.zhgcloud.refuelrecords.model.ZhgRefuelRecordsModel;

/**
 * @ClassName ZhgRefuelRecordsHandler
 * @Description 加油记录
 * @author linhuawei
 * @since 2021-11-09
 */
@Component
public class ZhgRefuelRecordsHandler {

    @Value("${zhgcloud.urls.refuelRecords}")
    private String refuelRecordsUrl;

    @Autowired
    private ZhgHandler zhgHandler;

    @Autowired
    private MachineInfoService machineInfoService;

    @Autowired
    private MachineRefuelRecordsService machineRefuelRecordsService;

    /**
     * @Title refuelRecords
     * @Description 获取加油记录
     * @param startDt 开始时间
     * @param endDt   结束时间
     * @return 加油记录
     */
//    @Async
    public void refuelRecords(String startDt, String endDt) {
        // 查询参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("count_per_page", 0);
        if (null != startDt) {
            params.put("start_dt", startDt);
        }
        if (null != endDt) {
            params.put("end_dt", endDt);
        }

        // 获取查询结果
        ZhgResultDataCallback callback = (JSONObject resultJson) -> {
            return resultJson.getString("data_list");
        };
        List<ZhgRefuelRecordsModel> collectList = zhgHandler.httpGetList(refuelRecordsUrl, params,
                ZhgRefuelRecordsModel.class, callback);
        if (ObjectUtils.isEmpty(collectList)) {
            return;
        }

        // 获取设备编号主键映射
        QueryWrapper<MachineInfoEntity> queryWrapper = new QueryWrapper<MachineInfoEntity>();
        queryWrapper.select("id", "machine_key");
        List<MachineInfoEntity> machineList = machineInfoService.list(queryWrapper);
        Map<String, String> machineKeyMap = new HashMap<String, String>();
        machineList.forEach(item -> {
            if (ObjectUtils.isNotEmpty(item.getMachineKey())) {
                machineKeyMap.put(item.getMachineKey(), item.getId());
            }
        });

        // 数据转化
        List<MachineRefuelRecordsEntity> list = new ArrayList<MachineRefuelRecordsEntity>();
        ZhgRefuelRecordsModel model;
        MachineRefuelRecordsEntity entity;
        BigDecimal volume;
        String machineId = null;
        for (int i = 0, len = collectList.size(); i < len; i++) {
            model = collectList.get(i);

            // 如果有用户输入数值，以用户输入数值作为当前油量
            volume = null == model.getCustom_volume() ? model.getVolume() : model.getCustom_volume();

            // 获取设备主键
            machineId = machineKeyMap.get(model.getMachine_key());

            // 加油记录数据对象转化
            entity = new MachineRefuelRecordsEntity();
            entity.setMachineId(machineId);
            entity.setMachineKey(model.getMachine_key());
            entity.setOriginalId(model.getRefuel_key());
            entity.setCollectDate(model.getStart_date_time());
            entity.setCollectVolume(model.getVolume());
            entity.setCustomDate(model.getCustom_date());
            entity.setCustomVolume(model.getCustom_volume());
            entity.setVolume(volume);
            list.add(entity);
        }

        // 保存加油记录
        if (ObjectUtils.isNotEmpty(list)) {
            machineRefuelRecordsService.saveBatch(list, 100);
        }
    }

}
